package com.itheima.dubbo.mongodb.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.commons.pojo.mongo.Comment;
import com.itheima.commons.vo.reception.PageResult;
import com.itheima.dubbo.interfaces.CommentApi;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @author yaohu
 * @date 2021/12/9
 * @Version 1.0
 * @describe
 */
@Service
public class CommentServiceImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 点赞 1 保存点赞
     *
     * @param comment
     * @return
     */
    @Override
    public Long likeComment(Comment comment) {
        mongoTemplate.save(comment);
        return queryLikeCount(comment.getPublishId().toHexString());
    }

    /**
     * 取消点赞 删除点赞
     *
     * @param userId
     * @param publishId 需要ObjectId
     * @return
     */
    @Override
    public Long disLikeComment(Long userId, String publishId) {
        mongoTemplate.remove(Query.query(Criteria
                .where("userId").is(userId)
                .and("publishId").is(new ObjectId(publishId))
                .and("commentType").is(1)), Comment.class);
        return queryLikeCount(publishId);
    }

    /**
     * 查询点赞数 1
     *
     * @param publishId
     * @return
     */
    @Override
    public Long queryLikeCount(String publishId) {
        return mongoTemplate.count(Query.query(Criteria
                        .where("publishId").is(new ObjectId(publishId))
                        .and("commentType").is(1)),
                Comment.class);
    }

    /**
     * 查询是否点赞
     *
     * @param userId
     * @param publishId
     * @return
     */
    @Override
    public Boolean queryUserIsLike(Long userId, String publishId) {
        List<Comment> commentList = mongoTemplate.find(Query.query(Criteria
                .where("userId").is(userId)
                .and("publishId").is(new ObjectId(publishId))
                .and("commentType").is(1)), Comment.class);
        return CollUtil.isNotEmpty(commentList);
    }

    /**
     * 喜欢 3
     *
     * @param comment
     * @return
     */
    @Override
    public Long loveComment(Comment comment) {
        mongoTemplate.save(comment);
        return queryLikeCount(comment.getPublishId().toHexString());
    }

    /**
     * 取消喜欢
     *
     * @param userId
     * @param publishId
     * @return
     */
    @Override
    public Long disLoveComment(Long userId, String publishId) {
        mongoTemplate.remove(Query.query(Criteria
                .where("userId").is(userId)
                .and("publishId").is(new ObjectId(publishId))
                .and("commentType").is(3)), Comment.class);
        return queryLoveCount(publishId);
    }

    /**
     * 查询喜欢数
     *
     * @param publishId
     * @return
     */
    @Override
    public Long queryLoveCount(String publishId) {
        return mongoTemplate.count(Query.query(Criteria
                        .where("publishId").is(new ObjectId(publishId))
                        .and("commentType").is(3)),
                Comment.class);
    }

    /**
     * 查询是否喜欢
     *
     * @param userId
     * @param publishId
     * @return
     */
    @Override
    public Boolean queryUserIsLove(Long userId, String publishId) {
        List<Comment> commentList = mongoTemplate.find(Query.query(Criteria
                .where("userId").is(userId)
                .and("publishId").is(new ObjectId(publishId))
                .and("commentType").is(3)), Comment.class);
        return CollUtil.isNotEmpty(commentList);
    }

    /**
     * 分页查询评论列表
     *
     * @param publishId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult queryCommentList(String publishId, Integer page, Integer pageSize) {
        List<Comment> commentList = mongoTemplate.find(Query.query(Criteria
                .where("publishId").is(new ObjectId(publishId))
                .and("commentType").is(2))
                .with(PageRequest.of(page - 1, pageSize)), Comment.class);

        return new PageResult(page, pageSize, queryCommentCount(publishId), commentList);
    }

    /**
     * 分页查询评论列表并排序
     * @param publishId  动态id
     * @param page
     * @param pageSize
     * @param sortProp   排序字段 默认评论人id
     * @param sortOrder  排序方式 默认降序
     * @return
     */
    @Override
    public PageResult queryCommentListBySort(String publishId, Integer page, Integer pageSize, String sortProp, String sortOrder) {
        List<Comment> commentList = mongoTemplate.find(Query.query(Criteria
                .where("publishId").is(new ObjectId(publishId))
                .and("commentType").is(2))
                .with(PageRequest.of(page - 1, pageSize))
                .with(Sort.by(Sort.Order.desc(sortProp)))
                , Comment.class);

        return new PageResult(page, pageSize, queryCommentCount(publishId), commentList);
    }

    /**
     * 保存评论 2
     *
     * @param comment
     * @return
     */
    @Override
    public Long saveComment(Comment comment) {
        mongoTemplate.save(comment);
        return queryLikeCount(comment.getPublishId().toHexString());
    }

    /**
     * 查询评论数
     *
     * @param publishId
     * @return
     */
    @Override
    public Long queryCommentCount(String publishId) {
        return mongoTemplate.count(Query.query(Criteria
                .where("publishId").is(new ObjectId(publishId))
                .and("commentType").is(2)), Comment.class);
    }

    /**
     * 根据ID查询quanzi_comment表
     *
     * @param publishId
     * @return
     */
    @Override
    public Comment queryCommentById(String publishId) {
        return mongoTemplate.findById(new ObjectId(publishId), Comment.class);
    }

    /**
     * 分页查询点赞/评论/喜欢列表
     *
     * @param page
     * @param pageSize
     * @param userId      用户id
     * @param commentType 1：点赞 2：评论 3：喜欢
     * @return
     */
    @Override
    public List<Comment> queryCommentListByUserId(Integer page, Integer pageSize, Long userId, int commentType) {
        return mongoTemplate.find(Query.query(Criteria.where("publishUserId").is(userId).and("commentType").is(commentType))
                .with(Sort.by(Sort.Order.desc("created")))
                .with(PageRequest.of(page - 1, pageSize)), Comment.class);
    }


    /**
     * 查询评论列表desc
     * @param userID
     * @param pagesize
     * @param page
     * @param sortProp
     * @param sortOrder
     * @param messageID
     * @return
     */
    @Override
    public PageResult getCommenctList(Object userID, String pagesize, String page, String sortProp, String sortOrder, String messageID) {
        //获得数据
        List<Comment> comments = mongoTemplate.find(Query.query(Criteria
                        .where("publishId").is(new ObjectId(messageID))
                        .and("commentType").is(2))
                        .with(PageRequest.of(Convert.toInt(page) - 1, Convert.toInt(pagesize)))
                        .with(Sort.by(Sort.Order.desc(sortProp)))
                , Comment.class);
        //获得总数
        long count = mongoTemplate.count(Query.query(Criteria
                        .where("publishId").is(new ObjectId(messageID))
                        .and("commentType").is(2))
                , Comment.class);
        return new PageResult(Convert.toInt(page), Convert.toInt(pagesize), count + Convert.toLong(pagesize), comments);

    }
//asc的排序方法获得评价列表
    @Override
    public PageResult getAscCommenctList(Object userID, String pagesize, String page, String sortProp, String sortOrder, String messageID) {
        //获得数据
        List<Comment> comments = mongoTemplate.find(Query.query(Criteria
                        .where("publishId").is(new ObjectId(messageID))
                        .and("commentType").is(2))
                        .with(PageRequest.of(Convert.toInt(page) - 1, Convert.toInt(pagesize)))
                        .with(Sort.by(Sort.Order.asc(sortProp)))
                , Comment.class);

        for (Comment comment : comments) {
            Long userId = comment.getUserId();

        }


        //获得总数
        long count = mongoTemplate.count(Query.query(Criteria
                        .where("publishId").is(new ObjectId(messageID))
                        .and("commentType").is(2))
                , Comment.class);
        return new PageResult(Convert.toInt(page), Convert.toInt(pagesize), count + Convert.toLong(pagesize), comments);

    }
}
